using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Reflection;

namespace WinUITest
{
    /// <summary>
    /// UI帮助类
    /// </summary>
    public static class UIHelper
    {
        /// <summary>
        /// 获得父级页面
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public static Page? GetParentPage(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            while (parent != null && parent is not Page)
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            if (parent is Page page) return page;
            return null;
        }
        /// <summary>
        /// 获得根控件
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public static Control? GetRootControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            Control? upParent = null;
            while (parent != null)
            {
                if (parent is Control control)
                {
                    upParent = control;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return upParent;
        }
        /// <summary>
        /// 根据成员路径获取值
        /// </summary>
        /// <param name="item"></param>
        /// <param name="memberPath"></param>
        /// <returns></returns>
        public static object? GetValueByMemberPath(object? item, string? memberPath)
        {
            if (item is null) return null;
            Type selectedItemType = item.GetType();
            if (string.IsNullOrWhiteSpace(memberPath)) return null;
            PropertyInfo? propertyInfo = selectedItemType.GetProperty(memberPath);
            if (propertyInfo is null) return null;
            object? value = propertyInfo.GetValue(item);
            return value;
        }
        /// <summary>
        /// 递归查找子元素
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="parent">父元素</param>
        /// <param name="childName">子元素名称</param>
        /// <returns>找到的子元素</returns>
        public static T? FindChildByName<T>(DependencyObject parent, string? childName = null) where T : FrameworkElement
        {
            if (parent is null) return null;
            T? foundChild = null;
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T t)
                {
                    if (!string.IsNullOrWhiteSpace(childName))
                    {
                        if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
                        {
                            foundChild = t;
                            break;
                        }
                    }
                    else
                    {
                        foundChild = t;
                        break;
                    }
                }
                foundChild = FindChildByName<T>(child, childName);
                if (foundChild != null) break;
            }

            return foundChild;
        }
    }
}
